<template>
  <el-form ref="form" :model="formOptions" :rules="formRules" class="mt20" label-width="100px">
    <el-form-item label="排序" prop="sort_id">
      <el-input-number v-model="formOptions['sort_id']"></el-input-number>
    </el-form-item>
    <el-form-item label="公司" prop="companyname" v-if="locationType==102">
      <el-select v-model="formOptions['companyname']" :remote-method="handleCompanySearch" filterable
                 remote clearable placeholder="请输入公司关键词" :loading="loading">
        <el-option v-for="(c,idx) in companyList" :key="idx" :label="c.companyname"
                   :value="c.companyname"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
import {SearchCompanyName} from "@/api/upload";

export default {
  name: "image-upload-form",
  props: {
    locationType: {
      type: Number | String,
      default: ''
    },
    formOptions: {
      type: Object,
      default: () => ({
        companyname: '',
        sort_id: ''
      })
    },
    formRules: {
      type: Object,
      default: () => ({
        sort_id: [{required: true, message: '排序必填'}],
        companyname: [{required: true, message: '公司必填'}]
      })
    }
  },
  data() {
    return {
      companyList: [],
      loading: false
    }
  },
  methods: {
    handleCompanySearch(query) {
      if (!query) return // 不查
      this.loading = true
      SearchCompanyName(query).then(res => {
        console.log('search results', res)
        this.companyList = res.result?.list_company || []
      }).finally(() => this.loading = false)
    },
    validate() {
      return this.$refs.form.validate()
    }
  }
}
</script>

<style scoped>

</style>
